package com.heima.service.iml;

import com.heima.mapper.DeptMapper;
import com.heima.mapper.EmpMapper;
import com.heima.poji.Dept;
import com.heima.poji.Result;
import com.heima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServieImp implements DeptService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
      List<Dept> li  = deptMapper.list();
      return li;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delectById(Integer id) {
        deptMapper.delectById(id);
        empMapper.delectByEmpId(id);
    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

    @Override
    public Dept selectByid(Integer id) {
        Dept dept=deptMapper.selectByid(id);
        return dept;
    }
}
